<template>
  <div>
    <el-dialog title="修改任务"
               :visible.sync="visible"
               :before-close="doCancel"
               :close-on-click-modal="false"
               :close-on-press-escape="false"
               width="60%"
               append-to-body>

      <el-form :model="task" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="姓名" prop="name" required>
          <el-input type="text" v-model="task.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="任务内容" prop="content" required>
          <el-input type="text" v-model="task.content" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="创建时间" prop="createTime" required>
          <el-date-picker type="date" placeholder="选择日期" v-model="task.createTime" style="width: 100%;"
                          value-format="yyyy-MM-dd" readonly></el-date-picker>
        </el-form-item>
        <el-form-item label="开始时间" prop="startTime">
          <el-date-picker type="date" placeholder="选择日期" v-model="task.startTime" style="width: 100%;"
                          value-format="yyyy-MM-dd"></el-date-picker>
        </el-form-item>
        <el-form-item label="结束时间" prop="endTime">
          <el-date-picker type="date" placeholder="选择日期" v-model="task.endTime" style="width: 100%;"
                          value-format="yyyy-MM-dd"></el-date-picker>
        </el-form-item>
        <el-form-item label="实际完成时间" prop="completionTime">
          <el-date-picker type="date" placeholder="选择日期" v-model="task.completionTime" style="width: 100%;"
                          value-format="yyyy-MM-dd"></el-date-picker>
        </el-form-item>
        <el-form-item label="完成状态" prop="status">
          <el-select v-model="task.status" placeholder="完成状态">
            <el-option label="未完成" value="1"></el-option>
            <el-option label="已完成" value="2"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <div style="text-align: center">
            <el-button type="primary" @click="submitForm('ruleForm')">保存</el-button>
            <el-button @click="resetForm('ruleForm')">重置</el-button>
          </div>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
  export default {
    name: "TaskAdd",
    props: {
      visible: {
        type: Boolean,
        default: false
      },
    },
    data() {
      return {
        //任务对象
        task: {},
        //表单校验规则
        rules: {
          name: [
            { required: true, message: '请输入姓名', trigger: 'blur' },
          ],
          content: [
            { required: true, message: '请输入活动内容', trigger: 'blur' },
          ]
        }
      }
    },
    methods: {
      //关闭弹窗
      doCancel() {
        this.resetForm("ruleForm");
        this.$emit("changeAddVisable", false)
      },
      //保存
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.$axios({
              method: "post",
              url: "task/saveOrUpdate",
              data: this.task
            }).then((res) => {
              console.log(res.data);
              // 如果返回code为1说明后台处理了请求  否则输出网络错误
              if (res.data.code == "1") {
                this.$alert("操作成功");
                this.$emit("query");
                this.doCancel();
              } else {
                alert("");
              }
            }).catch(err => {
              console.log(err);
            })
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      //重置
      resetForm(formName) {
        this.$refs[formName].resetFields();
      },
      //清空
      clearForm(){
        this.task = {}
      }
    }

  }
</script>

<style scoped>

</style>
